import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:like_button/like_button.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/extension/date_string_extension.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

import '../../../hj_model/comment/reply_model.dart';
import '../../../hj_utils/api_service/common_service.dart';
import '../../../hj_utils/date_time_util.dart';
import '../../../tools_base/widget/net_image_widget.dart';
import '../../home_business/widget/image_scale_view.dart';
import '../../user_center_page/user_center_page.dart';
import 'comment_item.dart';

class ReplyCommentItem extends StatelessWidget {
  final ReplyModel reply;
  final Function(ReplyModel)? replyHandler;
  late ValueNotifier<bool> likeValueNof;
  CommentItemConfig? config; //配置信息

  ReplyCommentItem(this.reply, {super.key, this.replyHandler, this.config})
      : likeValueNof = ValueNotifier(reply.isLike ?? false);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Container(height: 1, margin: EdgeInsets.only(bottom: 12, top: 12), color: config?.lineColor),
        16.sizeBoxH,
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            GestureDetector(
              onTap: () {
                if (reply.userID != null) {
                  Get.toNamed(UserCenterPage.routeName, arguments: {'uid': reply.userID ?? 0});
                }
              },
              child: NetworkImageLoader(
                imageUrl: reply.userPortrait ?? '',
                width: 24,
                height: 24,
                borderRadius: 18,
              ),
            ),
            12.sizeBoxW,
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        reply.userName ?? '',
                        style: TextStyle(
                          color: Color(0x73FFFFFF),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      3.sizeBoxW,
                      if (reply.toUserName?.isNotEmpty == true) ...[
                        Image.asset(
                          "arrow_grey.webp".commentPath,
                          width: 16,
                          height: 16,
                        ),
                        3.sizeBoxW,
                        Text(
                          reply.toUserName ?? '',
                          style: TextStyle(color: Color(0x73FFFFFF), fontSize: 14),
                        ),
                      ]
                    ],
                  ), // 还有个类似评分的东西
                  8.sizeBoxH,
                  Text(
                    reply.content ?? '',
                    style: TextStyle(
                      color: Color(0xE5FFFFFF),
                      fontSize: 14,
                    ),
                  ),
                  if (reply.image?.isNotEmpty == true)
                    GestureDetector(
                      onTap: () {
                        Get.to(ImageScaleView(url: reply.image, isPage: true));
                      },
                      child: Container(
                        margin: EdgeInsets.only(top: 6),
                        alignment: Alignment.centerLeft,
                        constraints: BoxConstraints(maxWidth: 150),
                        child: NetworkImageLoader(imageUrl: reply.image ?? ""),
                      ),
                    ),
                  8.sizeBoxH,
                  Row(
                    children: [
                      Text(
                        reply.createdAt?.utcYMDHM() ?? "",
                        style: const TextStyle(color: Color(0x73FFFFFF), fontSize: 12),
                      ),
                      const Spacer(),
                      ValueListenableBuilder(
                        valueListenable: likeValueNof,
                        builder: (context, value, child) => Row(
                          children: [
                            LikeButton(
                              isLiked: reply.isLike ?? false,
                              onTap: (isLiked) => likeAction(),
                              size: 20,
                              likeBuilder: (isLiked) {
                                return Image.asset(
                                    isLiked ? 'like_red.webp'.commentPath : 'like_white.webp'.commentPath);
                              },
                            ),
                            Text(
                              "${reply.likeCount?.humanizeFormatDefault("0") ?? "0"}",
                              style: TextStyle(
                                color: Color(0xE5FFFFFF),
                                fontSize: 12,
                              ),
                            ),
                          ],
                        ),
                      ),
                      12.sizeBoxW,
                      GestureDetector(
                        onTap: () {
                          if (replyHandler != null) {
                            replyHandler?.call(reply);
                          }
                        },
                        child: Container(
                          padding: EdgeInsets.only(right: 0),
                          child: Image.asset(
                            'msg_icon.png'.commentPath,
                            width: 20,
                            height: 20,
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }

  // 评论点赞
  Future<bool> likeAction() async {
    final islike = reply.isLike ?? false;
    bool result;
    if (!islike) {
      result = await CommonService.sendLike(reply.id ?? '', 'COMMENT');
      if (result) reply.likeCount = (reply.likeCount ?? 0) + 1;
    } else {
      result = await CommonService.cancelLike(reply.id ?? '', 'COMMENT');
      if (result) reply.likeCount = (reply.likeCount ?? 1) - 1;
    }
    if (result) reply.isLike = !islike;
    likeValueNof.value = reply.isLike!;
    return likeValueNof.value;
  }
}
